package com.example.demo2.controller;
import com.example.demo2.pojo.PageBean;
import com.example.demo2.pojo.vo.CategoryVO;
import com.example.demo2.utils.Result;
import com.example.demo2.pojo.vo.SupplierVO;
import com.example.demo2.service.SupplierService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/** 
 * <br/>
 * Created by weidong on 2020/02/16
 */
@Controller
public class SupplierController {

	@Resource
	private SupplierService supplierService;

	@RequestMapping("/supplier_list")
	public String supplier(String name, @RequestParam(defaultValue = "1") Integer currentPage, Model model) throws Exception {
		SupplierVO supplierVO = new SupplierVO();
		supplierVO.setEName(name);
		supplierVO.setCurrentPage(currentPage);
		PageBean pageBean=supplierService.listPage(supplierVO);
		model.addAttribute("pageBean", pageBean);
		model.addAttribute("query", supplierVO);
		return "supplier_list";
	}
	@RequestMapping("/supplier_list/add")
	@ResponseBody
	//添加
	public Result add(String suppliername,String supplierphone,String supplieraddress)throws Exception{
		SupplierVO supplierVO=new SupplierVO();
		supplierVO.setEName(suppliername);
		supplierVO.setEPhone(supplierphone);
		supplierVO.setEAddress(supplieraddress);
		return supplierService.add(supplierVO);
	}


	@RequestMapping("/supplier_list/delete/{id}")
	@ResponseBody
	//删除
	public Result deleteById(@PathVariable("id") Integer id)throws Exception{
		Result result = supplierService.deleteById(id);
		Integer code = result.getCode();
		if (code==200){
			supplierService.deleteById(id);
		}
		return result;
	}


	@RequestMapping("/supplier_list/update")
	@ResponseBody
	//更新
	public Result updateById(Integer id,String supplierName2,String supplierPhone2,String supplierAddress2)throws Exception{
		SupplierVO supplierVO=new SupplierVO();
		supplierVO.setId(id);
		supplierVO.setEName(supplierName2);
		supplierVO.setEPhone(supplierPhone2);
		supplierVO.setEAddress(supplierAddress2);
		return supplierService.updateById(supplierVO);
	}
	@RequestMapping("/supplier_list/find/{id}")
	@ResponseBody
	//更新回显
	public Result findClass(@PathVariable("id")Integer id)throws Exception{

		return supplierService.findById(id);
	}



}